package GreedyAlgorithm;
/*
 * 监控二叉树
 */
import BinaryTree.init.TreeNode;

public class lc968 {

    int res = 0;
    public int minCameraCover(TreeNode root) {
        if (backtracking(root) == 0) {
            res++;
        }
        return res;
    }
    public int backtracking(TreeNode cur) {
        if (cur == null) return 2;
        int left = backtracking(cur.left);
        int right = backtracking(cur.right);
        if (left == 2 && right == 2) {
            return 0;
        }
        if (left == 0 || right == 0) {
            res++;
            return 1;
        }
        if (left == 1 || right == 1) {
            return 2;
        }
        return -1;
    }

}
